package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/addressBook")
@Slf4j
@Api(tags = "用户地址信息相关接口")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 查询用户的地址信息
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询用户地址信息")
    public Result<List<AddressBook>> list(){
        AddressBook address = new AddressBook();
        address.setUserId(BaseContext.getCurrentId());
        List<AddressBook> addressList = addressBookService.list(address);
        return Result.success(addressList);
    }

    /**
     * 新增地址信息
     * @param addressBook
     * @return
     */
    @PostMapping
    @ApiOperation("新增地址")
    public Result save(@RequestBody AddressBook addressBook){
        log.info("新增的地址信息：{}",addressBook);
        addressBookService.save(addressBook);
        return Result.success();
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询")
    public Result<AddressBook> getById(@PathVariable Long id){
        log.info("查询的id：{}",id);
        AddressBook address = addressBookService.getById(id);
        return Result.success(address);
    }

    /**
     * 修改地址信息
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("修改地址信息")
    public Result updateAddress(@RequestBody AddressBook addressBook){
        addressBookService.updateAddress(addressBook);
        return Result.success();
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
     public Result setDefault(@RequestBody AddressBook addressBook){
        addressBookService.setDefault(addressBook);
        return Result.success();
     }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("根据id删除地址")
     public Result deleteById(Long id){
        log.info("要删除的地址id：{}",id);
        addressBookService.deleteById(id);
        return Result.success();
     }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    @ApiOperation("查询默认地址")
     public Result<AddressBook> getDefault(){
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(1);
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> list = addressBookService.list(addressBook);
        for (AddressBook book : list) {
            if(book.getIsDefault() == 1){
                return Result.success(book);
            }
        }
        return Result.error("没有查到默认地址");
    }
}
